﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Controls;
using DataAccess;

namespace SPIWareMVVM.Views.Conversions
{
    public class RunTimeConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            if (values.Count() == 2 && values[0].ToString() != "{DependencyProperty.UnsetValue}")
            {
                DateTime startTime = (DateTime)values[0];
                DateTime endTime = (DateTime)values[1];
                return (endTime - startTime).ToString();
            }
            return string.Empty;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class RunTimeGatherer
    {
        public string Convert(ListBox lbProduction)
        {
            TimeSpan runTime = new TimeSpan();
            foreach (MachineProduction mp in lbProduction.Items)
                runTime += (DateTime)mp.EndTime - (DateTime)mp.StartTime;
            return runTime.ToString();
        }
    }

    public class ShotsMadeConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            if (values.Count() == 2 && values[0].ToString() != "{DependencyProperty.UnsetValue}")
            {
                int startCount = (int)values[0];
                int endCount = (int)values[1];
                return (endCount - startCount).ToString();
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class PartsCountConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            ////(endcount - begcount) * partspershot - startPartsInBox + endPartsInBox
            //if (values.Count() == 5 && values[2].ToString() != string.Empty)
            //{
            //    return (((int)values[0] - (int)values[1]) * Int32.Parse(values[2].ToString()) - (int)values[3] + (int)values[4]).ToString();
            //}
            //(endcount - begcount) * partspershot
            if (values.Count() == 3 
                && values[0].ToString() != "{DependencyProperty.UnsetValue}" 
                && values[2].ToString() != string.Empty)
                return ((Int32.Parse(values[0].ToString()) - Int32.Parse(values[1].ToString())) * Int32.Parse(values[2].ToString())).ToString();
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class ScrapCountConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            if (values.Count() == 8 && values[0].ToString() != "{DependencyProperty.UnsetValue}")
            {
                int total = 0;
                foreach (int val in values)
                    total += val;
                return total.ToString();
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class GoodCountConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            ////PartsCount - ScrapCount
            //if (values.Count() == 2 && values[0].ToString() != "{DependencyProperty.UnsetValue}" && values[0].ToString() != string.Empty)
            //{
            //    return (Int32.Parse(values[0].ToString()) - Int32.Parse(values[1].ToString())).ToString();
            //}

            //Good Parts = (endcount - begcount) * ppShot - scrap
            if (values.Count() == 4 && values[0].ToString() != "{DependencyProperty.UnsetValue}" && values[2].ToString() != string.Empty && values[3].ToString() != string.Empty)
            {
                return ((Int32.Parse(values[0].ToString()) - Int32.Parse(values[1].ToString())) * Int32.Parse(values[2].ToString()) - Int32.Parse(values[3].ToString())).ToString();
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class BalanceConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            //Balance = (Shots Made * ppShot) - (Boxes * ppBox - begPPBox + endPPBox + scrap)
            if (values.Count() == 7 && values[0].ToString() != "{DependencyProperty.UnsetValue}" 
                && values[6].ToString() != "{DependencyProperty.UnsetValue}" 
                && values[1].ToString() != string.Empty)
            {
                //return (Int32.Parse(values[0].ToString()) * Int32.Parse(values[1].ToString()) - Int32.Parse(values[2].ToString()) + Int32.Parse(values[3].ToString())).ToString();
                int boxes = Int32.Parse(values[0].ToString());
                int ppBox = Int32.Parse(values[1].ToString());
                int begPPBox = Int32.Parse(values[2].ToString());
                int endPPBox = Int32.Parse(values[3].ToString());
                int shotsMade = Int32.Parse(values[4].ToString());
                int scrap = Int32.Parse(values[5].ToString());
                int ppShot = Int32.Parse(values[6].ToString());
                return ((shotsMade * ppShot) - (boxes * ppBox - begPPBox + endPPBox + scrap)).ToString();
            }
            return string.Empty;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
